
import { newSessionid, getCatesian3FromPX ,createRemindertip,destroyTip} from "../../plotCommon";
const Cesium = window.Cesium;
import CreateArrow from './CreateArrow'
import EditArrow from './editArrow'
export default class Arrow {
    handler:any;
    _viewer:any
    editArrowClass:any
    pickerEntity:any
    _option:any
    _entities:Array<any>
constructor(viewer){
    this._viewer=viewer
this._entities=[]
}
get options() {
    return this._option;
  }
set options(val){
    val=Object.assign({},val)
  this._option=val
}
get entitis(){
  return this._entities
}
set entitis(val){
  this._entities=val
}
create(){
    let viewer= this._viewer
    if (this.handler) this.handler.destroy();
    this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    const resultList = [];
    const callback = (event) => {
   this._entities.push(event)
    };
    CreateArrow(viewer, this.handler, resultList, this._option, callback);
}
stopCreate(){
    destroyTip();
    this.handler.destroy();
}
edit(editCalback){
    let viewer= this._viewer

    this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    const calback = (e) => {
      if (editCalback && typeof editCalback == "function")editCalback(e)
      if (this.editArrowClass) this.editArrowClass.clear();
      this.getPicking(this.handler, viewer, calback, false);
      this.editArrowClass = new EditArrow(e, viewer, this.handler);
      this.editArrowClass.startEdit();  
    };
    const openTripParemeter = "code";
    this.getPicking(this.handler, viewer, calback, false);
}
stopEdit(){
    if (this.handler) this.handler.destroy();
    if (this.editArrowClass){this.editArrowClass.stopEdit();

 this.editArrowClass.clear()}
}
removeById(id){
    this._viewer.entities.removeById(id)
    this.stopEdit()
  this._entities=this._entities.filter(item=>item.id!=id)
  
}
getPicking(handler, viewer, callback, openTripParemeter) {
    let _this = this;
    handler.setInputAction(function (click) {
      var position = click.position;
      var pickedObject = viewer.scene.pick(position);
      // 检查pickedObject是否为实体
      if (
        Cesium.defined(pickedObject) &&
        Cesium.defined(pickedObject.id) &&
        pickedObject.id instanceof Cesium.Entity
      ) {
        // 获取点击的实体+
        var entity = pickedObject.id;
        // 输出实体信息
        const toolTip = pickedObject.id;
        _this.pickerEntity = entity;
        if (callback && typeof callback == "function") callback(entity);
        if (openTripParemeter && typeof openTripParemeter == "string")
          createRemindertip(
            entity[openTripParemeter],
            click.position,
            true
          );
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }
}